#ifndef _C_LIGHT_H_
#define _C_LIGHT_H_

#include "../cuCommon/cuTypes.h"
#include "../interface/I_Buffer.h"
#include "../interface/T_Buffer_GL.h"
#include "../interface/I_api_Device.h"

struct S_PointLight
{
	union
	{
		float4 position_radius;
		struct 
		{
			float3	position;
			float		radius;
		}; 
	};
	float4 color;

	S_PointLight(float4 pos, float4 clr)
	{
		position_radius = pos;
		color = clr;
	}

	S_PointLight(float3 pos, float r, float4 clr)
	{
		position = pos;
		radius = r;
		color = clr;
	}

	S_PointLight(float3 pos, float r, float3 clr, float w = 1.0f)
	{
		position = pos;
		radius = r;
		color = f4(clr, w);
	}
};

class C_LightManager_GL
{
private:
	std::vector<S_PointLight> m_PointLightList;

	I_Buffer	*	m_pPosition;
	I_Buffer	*	m_pColor;

	int					m_Index;

public:
	C_LightManager_GL()
	{
		m_pPosition = NULL;
		m_pColor = NULL;

		m_Index = 0;
	}

	~C_LightManager_GL()
	{
		delete m_pPosition;
		delete m_pColor;

		m_PointLightList.clear();
	}

	void Create(int nLightCount, I_api_Device * pDevice = NULL)
	{
		m_PointLightList.reserve(nLightCount);

		if (m_pPosition == NULL)
			m_pPosition = new T_Buffer_GL<float4>();

		m_pPosition->Create(nLightCount, pDevice);

		if (m_pColor == NULL)
			m_pColor = new T_Buffer_GL<float4>();

		m_pColor->Create(nLightCount, pDevice);

		m_Index = 0;
	}

	void Release()
	{
		m_pPosition->Release();
		m_pColor->Release();

		m_PointLightList.clear();
	}

	S_PointLight	*	Get(int iLight)
	{
		if (iLight < (int) m_PointLightList.size())
			return &m_PointLightList[iLight];
	}
	
	void						Add(float4 position, float4 color)
	{
		m_PointLightList.push_back(S_PointLight(position, color));

		float4 *p = (float4 *) m_pPosition->hPtr();
		float4 *c = (float4 *) m_pColor->hPtr();

		p[m_Index] = position;
		c[m_Index] = color;
		m_Index++;
	}

	void						Add(float3 pos, float r, float4 color)
	{
		m_PointLightList.push_back(S_PointLight(pos, r, color));

		float4 *p = (float4 *) m_pPosition->hPtr();
		float4 *c = (float4 *) m_pColor->hPtr();

		p[m_Index] = f4(pos, r);
		c[m_Index] = color;
		m_Index++;
	}

	void						ResetLightIndex()
	{
		m_Index = 0;
	}
};

#endif